<?php
/**
 * Created by PhpStorm.
 * User: 张良
 * Date: 2018/1/11
 * Time: 16:05
 */

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\CommonController;
use App\Models\Admin;
use App\Models\User;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Session;
use Request;
use Redirect;

class AdminExamineController extends CommonController
{
    protected $pageNum = 15;
    public function login()
    {
        return view('adminExamine/login');
    }

    public function doLogin()
    {
        $input = Request::all();
        $account = $input['account'];
        $password = md5($input['password']);
        $user=Admin::where(['type'=>5])->first();
        if(empty($user)){
            $user['account']='admin';
            $user['password']=md5('123456');
        }
        if ($account == $user['account'] && $password == $user['password']) {
            return Redirect::to('Admin/admin-examine/adminExamineHome');
        } else {
            return back()->with('error', '用户名或密码错误');
        }
    }

    public function loginOut()
    {
        session()->flush();
        return Redirect::to('Admin/admin-examine/adminExamineLogin');
    }

    public function index()
    {
        $user = User::orderBy('created_at', 'desc')->get();
        $userCount = count(ota($user));
        $dealingUserCount = DB::table('user')->where(array('review' => 0))->count();
        $dealedUserCount = DB::table('user')->where(array('review' => 1))->count();
        $data = [
            'user'=>$user,
            'userCount' => $userCount,
            'dealingUserCount' => $dealingUserCount,
            'dealedUserCount' => $dealedUserCount
        ];
        return view('adminExamine/index')->with('data', $data);
    }

    public function review($id, $type)
    {
        $check = object_to_array(DB::table('user')->where(array("id" => $id))->first());
        if (!empty($check)) {
            if ($type != 1) {
                $date['review'] = 1;
                $res = DB::table('user')->where(array("id" => $id))->update($date);
                if ($res) {
                    return back()->with('success', '用户成功通过审核');
                } else {
                    return back()->with('error', '用户通过审核失败');
                }
            } else {
                return back()->with('error', '该用户已通过审核');
            }
        } else {
            return back()->with('error', '查无此用户');
        }
    }

    public function delete($id)
    {
        $check = object_to_array(DB::table('user')->where(array("id" => $id))->first());
        if (!empty($check)) {
            $res = DB::table('user')->where(array("id" => $id))->delete();
            if ($res) {
                return back()->with('success', '删除成功');
            } else {
                return back()->with('error', '删除失败');
            }
        } else {
            return back()->with('error', '查无此用户');
        }
    }
}